package cn.ly.security.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

@Aspect
@Component
public class AspectCreateTime {
    @Pointcut("execution(* cn.ly.security.dao..*.insert(..))")
    private void myPointCut() {
    }


    @Before("myPointCut()")
    public void myBefore(JoinPoint joinPoint) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        System.out.print("前置通知，目标：");
        System.out.print(joinPoint.getTarget() + "方法名称:");
        System.out.println(joinPoint.getSignature().getName());
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            System.out.println(arg);
            System.out.println(arg.getClass());
            Class<?> aClass = arg.getClass();
            Method[] methods = aClass.getDeclaredMethods();
            for (Method method : methods) {
                if (method.getName().contains("CreateTime")&&method.getName().contains("set")){
                    System.out.println(method.getName());
                    method.invoke(arg,new Date());
                }
            }
        }
    }
}
